package com.spa.interfaces.member;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.spa.application.command.member.*;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.member.*;
import com.spa.application.query.PageQuery;
import com.spa.application.query.member.MemberAddressesQuery;
import com.spa.application.query.member.MemberFeedbackQuery;
import com.spa.application.query.member.MemberMassagistCollectionQuery;
import com.spa.application.query.member.SmsQuery;
import com.spa.application.service.member.MemberMassagistApplicationService;
import com.spa.application.service.member.MemberMyApplicationService;
import com.spa.application.service.member.MemberUserApplicationService;
import com.spa.infrastructure.common.ResponseMessage;
import com.spa.infrastructure.enums.dict.base.RequestSourceEnum;
import com.spa.infrastructure.service.sms.GenerateSmsCodeKey;
import com.spa.infrastructure.service.sms.SmsProvider;
import com.spa.interfaces.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.List;

@RestController
@RequestMapping("member/my")
public class MemberMyController extends BaseController {

    @Resource
    private MemberUserApplicationService userApplicationService;

    @Resource
    private MemberMassagistApplicationService memberMassagistApplicationService;

    @Resource
    private MemberMyApplicationService memberMyApplicationService;

    @Resource
    private GenerateSmsCodeKey smsCodeKey;

    @Resource
    private SmsProvider smsProvider;

    @PostMapping("/addresses/page")
    public ResponseMessage<PageResult<MemberAddressesDTO>> page(@RequestBody MemberAddressesQuery query) {
        return responseHelper.success(memberMyApplicationService.page(query));
    }

    @PostMapping("/sendSms4Address")
    public ResponseMessage sendSms4Address(@Validated @RequestBody SmsQuery query) {
        String key = smsCodeKey.generateSmsCodeKey(query.getPhone(), RequestSourceEnum.Member_Address.getCode());
        smsProvider.sendVerificationCode(query.getPhone(), key);
        return responseHelper.success();
    }

    @PostMapping("/addresses/add")
    public ResponseMessage<MemberAddressesDTO> add(@Validated @RequestBody AddressesAddCommand command) {
        return responseHelper.success(memberMyApplicationService.addAddress(command));
    }

    @PostMapping("/addresses/edit")
    public ResponseMessage edit(@Validated @RequestBody AddressesEditCommand command) {
        memberMyApplicationService.editAddress(command);
        return responseHelper.success();
    }

    @PostMapping("/addresses/delete")
    public ResponseMessage delete(@Validated @RequestBody AddressesDeleteCommand command) {
        memberMyApplicationService.delAddress(command);
        return responseHelper.success();
    }

    @PostMapping("/addresses/editDefault")
    public ResponseMessage editDefault(@Validated @RequestBody AddressesEditDefaultCommand command) {
        memberMyApplicationService.editDefault(command);
        return responseHelper.success();
    }

    /**
     * 我的-获取个人信息
     * @return
     */
    @PostMapping("/info")
    @SaIgnore
    public ResponseMessage<MemberUserInfoDTO> info() {
        if (!StpUtil.isLogin()) {
            MemberUserInfoDTO dto = new MemberUserInfoDTO();
            dto.setAddressCount(0);
            dto.setCouponCount(0);
            dto.setCollectionCount(0);
            return responseHelper.success(dto);
        }
        return responseHelper.success(userApplicationService.userInfo());
    }

    @PostMapping("/edit")
    public ResponseMessage edit(@Validated @RequestBody MemberUserInfoEditCommand command) {
        userApplicationService.editUserInfo(command);
        return responseHelper.success();
    }

    @PostMapping("/ticket/queryMyTicketList")
    public ResponseMessage<List<MemberTicketDTO>> queryMyTicketList() {
        return responseHelper.success(memberMyApplicationService.listByUserId());
    }

    @PostMapping("/collection/myList")
    public ResponseMessage<List<MemberMassagistDTO>> collectionMyList(@RequestBody MemberMassagistCollectionQuery query) {
        return responseHelper.success(memberMassagistApplicationService.pageMyMassagistCollection(query));
    }

    @PostMapping("/feedback/addFeedback")
    public ResponseMessage addFeedback(@Validated @RequestBody MemberFeedbackAddCommand command) {
        memberMyApplicationService.addFeedBack(command);
        return responseHelper.success();
    }

    @PostMapping("/feedback/pageFeedBack")
    public ResponseMessage<PageResult<MemberFeedbackDTO>> pageFeedBack(@Validated @RequestBody MemberFeedbackQuery query) {
        return responseHelper.success(memberMyApplicationService.pageFeedBack(query));
    }

    @PostMapping("/scanCode")
    public ResponseMessage<ScanCodeDTO> scanCode(@Validated @RequestBody ScanCodeCommand command) throws MalformedURLException, UnsupportedEncodingException {
        return responseHelper.success(memberMyApplicationService.scanCode(command.getUrl()));
    }

    @PostMapping("/exchange")
    public ResponseMessage exchange(@Validated @RequestBody ExchangeCommand command) throws JsonProcessingException {
        memberMyApplicationService.exchange(command);
        return responseHelper.success();
    }

    @PostMapping("/coupons")
    public ResponseMessage<PageResult<CouponDTO>> coupons(@RequestBody PageQuery query) {
        return responseHelper.success(memberMyApplicationService.page(query));
    }

}
